package com.qingxin.park.action;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.park.entity.MaterialProduct;
import com.qingxin.park.service.MaterialProductService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;

@Controller
@RequestMapping("/materialProduct")
public class MaterialProductAction extends BaseAction {

	@Autowired
	private MaterialProductService materialProductService;
	
	
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "/trace/manufacturer/materialProduct_list";
	}
	
	/**
	 * 类型列表分页数据
	 * @param leftdata
	 * @param datatype
	 * @param materialName
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object typelistdata(String leftdata,String search,PageParam pageParam, HttpServletRequest request){
		Pager pager = materialProductService.list(leftdata,search, pageParam, getLoginUser(request));
		return pager;	
	}
	/**
	 * 添加数据
	 * @param model
	 * @param leftdata
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do" , method=RequestMethod.GET)
	public String add(ModelMap model, String leftdata, HttpServletRequest request){
		model.put("manufacturerid", leftdata);
		return "trace/manufacturer/materialProduct_add";
	}
	
	/**
	 * 保存添加
	 * @param materialProduct
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid MaterialProduct materialProduct, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = materialProductService.add(materialProduct, getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 更新数据
	 * @param model
	 * @param materialid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do" , method=RequestMethod.GET)
	public String edit(ModelMap model, String materialid, HttpServletRequest request){
		MaterialProduct materialProduct = materialProductService.get(materialid);
		model.put("materialProduct", materialProduct);
		return "trace/manufacturer/materialProduct_edit";
	}
	
	/**
	 * 保存更新
	 * @param materialProduct
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid MaterialProduct materialProduct, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = materialProductService.update(materialProduct, getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 删除
	 * @param materialid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] materialid, HttpServletRequest request){
		if(materialid==null || materialid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return materialProductService.delete(materialid, getLoginUser(request), request);
	}
	

}
